(function($) {

$.fn.fixedHeader = function (options) {
 var config = {
   topOffset: 38,
   headerContainer:$("#headerContainer"),
   columnContainer:$("#columnContainer"),
   colheadContainer:$("#colheadContainer")
   //bgColor: 'white'
 };
 if (options){ $.extend(config, options); }

 return this.each( function() {
  var o = $(this);
  
  var tar = config.headerContainer;
  var col = config.columnContainer;
  var clhd = config.colheadContainer;
  var $pwin = $(this).parent();
  var $win = $(window)
    , $head = $('thead.header', o)
    , isFixed = 0;
  var headTop = $head.length && $head.offset().top - config.topOffset;
  
  var leftWidth = 0;
  o.find('thead.header > tr > th.fixed-column').each(function(){
	  leftWidth += this.offsetWidth;
  });
  leftWidth+=1;
  function processScroll() {
    if (!o.is(':visible')) return;
    if ($('thead.header-copy').size()) {
      tar.find("table").width($head.width());
      clhd.width(leftWidth);
      var i, scrollTop = $win.scrollTop();
    }
    var t = $head.length && $head.offset().top - config.topOffset;
    if (!isFixed && headTop != t) { headTop = t; }
    if (scrollTop >= headTop && !isFixed) {
      isFixed = 1;
    } else if (scrollTop <= headTop && isFixed) {
      isFixed = 0;
    }
    isFixed ? tar.show().find("table").offset({ left: $head.offset().left })
            : tar.hide();
    isFixed ? clhd.show(): clhd.hide();
    // NG: dislocate while iframe page resized. fixed by jeffen@pactera 2015/7/8
	headerCopyRectify();
	tar.width($pwin.width());
  }
  
  // set a broken bone when header copy dislocated
  function headerCopyRectify() {
    o.find('thead.header > tr > th').each(function (i, h) {
      var w = $(h).width();
      tar.find('thead.header-copy> tr > th:eq('+i+')').width(w);
      clhd.find('thead.header-copy> tr > th:eq('+i+')').width(w)
    });
  }
  
  var pstate = o.data("fixed-header");
  if(pstate){
	  processScroll();
	  return;
  }
  o.data("fixed-header",true);
  
  $pwin.on('scroll', processScroll);
  $win.on('scroll', processScroll);
  // NG: dislocate while body resized. fixed by jeffen@pactera 2015/7/9
  $win.on('resize', processScroll);

  // hack sad times - holdover until rewrite for 2.1
  $head.on('click', function () {
    if (!isFixed) setTimeout(function () {  $win.scrollTop($win.scrollTop() - 47)}, 10);
  });

  clhd.find("table").html($head.clone(true).removeClass('header').addClass('header-copy header-fixed'));
  clhd.css({'position': 'fixed', 'top': config['topOffset']});
  clhd.find('thead.header-copy').width($head.width());
  tar.find("table").html($head.clone(true).removeClass('header').addClass('header-copy header-fixed'));
  tar.css({'position': 'fixed', 'top': config['topOffset']});
  tar.find('thead.header-copy').width($head.width());
  col.html(o.clone()).width(leftWidth).css("position","absolute").css("top",0).height(o.height());
  
  headerCopyRectify();
  $head.css({ margin:'0 auto',
              width: o.width(),
             'background-color':config.bgColor });
  processScroll();
  

 });
};

})(jQuery);
